package qc.module.ehs.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import qc.common.core.constants.QCAuthConstant;
import qc.common.core.exception.QCPromptException;
import qc.module.ehs.dto.taskbench.TaskBenchQueryConditionDto;
import qc.module.ehs.dto.taskitem.TaskItemPaginationQueryResultDto;
import qc.module.ehs.service.EhsTaskItemService;
import qc.module.platform.dto.uac.UacUserInfoDto;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * EHS 任务工作台Controller
 *
 * @author QuCheng Tech
 * @since 2024/11/13
 */
@RestController
@RequestMapping("/taskbench")
public class TaskBenchController {
    
    @Autowired
    private EhsTaskItemService taskItemService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    private RestTemplatePlatform restTemplatePlatform;

    /**
     * 我的待办
     *
     * 获取个人的待办任务列表；后台接口不管DTO中的任务状态标记；
     * 查询的结果按照计划截止时间升序排列；
     *
     * @param condition 查询条件
     * @return TaskBenchQueryResultDto
     * @author QuCheng Tech
     * @since 2024/11/15
     */
    @RequestMapping(value = "/todo", method = {RequestMethod.POST, RequestMethod.GET})
    public TaskItemPaginationQueryResultDto queryTODO(@RequestBody TaskBenchQueryConditionDto condition) throws QCPromptException {
        //查询个人的待办需要获取用户ID及用户作为负责人的部门ID集合
        HttpHeaders headers = new HttpHeaders();
        headers.add(QCAuthConstant.TOKEN_HEADER_KEY, request.getHeader(QCAuthConstant.TOKEN_HEADER_KEY));
        headers.add(QCAuthConstant.PROJECT_CODE_HEADER_KEY, request.getHeader(QCAuthConstant.PROJECT_CODE_HEADER_KEY));

        Integer userId;
        int[] deptIds = null;//用户负责的部门ID集合

        UacUserInfoDto userInfo = restTemplatePlatform.getUserInfo(headers);
        if (Objects.nonNull(userInfo)) {
            userId = userInfo.getId();
        } else {
            userId = null;
        }

        //验证是否有用户ID
        if (userId == null || userId < 0x1)
            throw new QCPromptException("查询待办任务列表，根据用户凭据获取不到有效的用户ID");

        //获取当前用户负责的部门ID
        List<Integer> userLeaderDeptIds = restTemplatePlatform.getProjectDeptLeaders(userId.intValue(), headers);
        if (userLeaderDeptIds != null && userLeaderDeptIds.size() > 0x0) {
            deptIds = new int[userLeaderDeptIds.size()];
            for (int i = 0; i < userLeaderDeptIds.size(); i++) {
                deptIds[i] = userLeaderDeptIds.get(i).intValue();
            }
        }

        condition.setFlag(0x0);//查询待办

        return taskItemService.query(condition, userId, deptIds);
    }

    /**
     * 查询任务
     *
     * 提供给个人工作台查询待办、已办任务；支持分页查询，按任务项的计划截止时间降序；
     * 查询的表均来源于任务项表；所有的任务、任务分组在派单时均由后台自动生成任务项；
     *
     * @param condition 查询条件
     * @return TaskBenchQueryResultDto
     * @author QuCheng Tech
     * @since 2024/11/15
     */
    @RequestMapping(value = "/query", method = {RequestMethod.POST, RequestMethod.GET})
    public TaskItemPaginationQueryResultDto query(@RequestBody TaskBenchQueryConditionDto condition) throws QCPromptException {
        //查询个人的任务（待办、已办）需要获取用户ID及用户作为负责人的部门ID集合
        HttpHeaders headers = new HttpHeaders();
        headers.add(QCAuthConstant.TOKEN_HEADER_KEY, request.getHeader(QCAuthConstant.TOKEN_HEADER_KEY));
        headers.add(QCAuthConstant.PROJECT_CODE_HEADER_KEY, request.getHeader(QCAuthConstant.PROJECT_CODE_HEADER_KEY));

        Integer userId;
        int[] deptIds = null;//用户负责的部门ID集合

        UacUserInfoDto userInfo = restTemplatePlatform.getUserInfo(headers);
        if (Objects.nonNull(userInfo)) {
            userId = userInfo.getId();
        } else {
            userId = null;
        }

        //验证是否有用户ID
        if (userId == null || userId < 0x1)
            throw new QCPromptException("查询待办任务列表，根据用户凭据获取不到有效的用户ID");

        //获取当前用户负责的部门ID
        List<Integer> userLeaderDeptIds = restTemplatePlatform.getProjectDeptLeaders(userId.intValue(), headers);
        if (userLeaderDeptIds != null && userLeaderDeptIds.size() > 0x0) {
            deptIds = new int[userLeaderDeptIds.size()];
            for (int i = 0; i < userLeaderDeptIds.size(); i++) {
                deptIds[i] = userLeaderDeptIds.get(i).intValue();
            }
        }

        return taskItemService.query(condition, userId, deptIds);
    }
}
